﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class InsertTable : ICommandUIDefinition, IControlsChild, IXmlBuildable
    {
        public string Alt { get; set; }
        public string CommandPreview { get; set; }
        public string CommandRevert { get; set; }
        public Enumerations.CommandType? CommandType { get; set; }
        public string Id { get; set; }
        public string MenuSectionInitialTitle { get; set; }
        public string MenuSectionTitle { get; set; }
        public int? Sequence { get; set; }

        public InsertTable()
        {}

        public InsertTable(XmlNode node):this()
        {
            try
            {
                this.Alt = XmlHelper.GetAttribute(node, "Alt");
                this.CommandPreview = XmlHelper.GetAttribute(node, "CommandPreview");
                this.CommandRevert = XmlHelper.GetAttribute(node, "CommandRevert");
                this.CommandType = (Enumerations.CommandType)Enum.Parse(typeof(Enumerations.CommandType), XmlHelper.GetAttribute(node, "CommandType"));
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.MenuSectionInitialTitle = XmlHelper.GetAttribute(node, "MenuSectionInitialTitle");
                this.MenuSectionTitle = XmlHelper.GetAttribute(node, "MenuSectionTitle");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode insertTableNode = XmlHelper.AddNode(commandUINode, "InsertTable");
                XmlHelper.AddAttribute(insertTableNode, "Alt", this.Alt);
                XmlHelper.AddAttribute(insertTableNode, "CommandPreview", this.CommandPreview);
                XmlHelper.AddAttribute(insertTableNode, "CommandRevert", this.CommandRevert);
                XmlHelper.AddAttribute(insertTableNode, "CommandType", this.CommandType);
                XmlHelper.AddAttribute(insertTableNode, "Id", this.Id);
                XmlHelper.AddAttribute(insertTableNode, "MenuSectionInitialTitle", this.MenuSectionInitialTitle);
                XmlHelper.AddAttribute(insertTableNode, "MenuSectionTitle", this.MenuSectionTitle);
                XmlHelper.AddAttribute(insertTableNode, "Sequence", this.Sequence);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("InsertTable");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, null, true);

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
